package jnpf.controller;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.service.VisualdevService;
import jnpf.base.util.VisualUtils;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.entity.ZXGL_RenWuZhuanBanEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.generater.util.GeneraterSwapUtil;
import jnpf.model.common.ZXGL_FlowWriteBack;
import jnpf.model.zxgl_renwuzhuanban.*;
import jnpf.service.ZXGL_RenWuZhuanBanService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * ZXGL_RenWuZhuanBan
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-22
 */
@Slf4j
@RestController
@Api(tags = "任务转办", value = "zxgl")
@RequestMapping("/ZXGL_RenWuZhuanBan")
public class ZXGL_RenWuZhuanBanController {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_RenWuZhuanBanService zXGL_RenWuZhuanBanService;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;


    /**
     * 列表
     *
     * @param zXGL_RenWuZhuanBanPagination
     * @return
     */
    @ApiOperation("任务转办分页")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_RenWuZhuanBanPagination zXGL_RenWuZhuanBanPagination) throws IOException {
        List<ZXGL_RenWuZhuanBanEntity> list = zXGL_RenWuZhuanBanService.getList(zXGL_RenWuZhuanBanPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_RenWuZhuanBanEntity entity : list) {
            entity.setSqr(generaterSwapUtil.userSelectValues(entity.getSqr()));
            entity.setSqbm(generaterSwapUtil.comSelectValues(entity.getSqbm()));
            entity.setZbr(generaterSwapUtil.userSelectValues(entity.getZbr()));
            entity.setBzbr(generaterSwapUtil.userSelectValues(entity.getBzbr()));
            entity.setZbrbm(generaterSwapUtil.comSelectValues(entity.getZbrbm()));
            entity.setBzbrbm(generaterSwapUtil.comSelectValues(entity.getBzbrbm()));
            entity.setZbbmfzr(generaterSwapUtil.userSelectValues(entity.getZbbmfzr()));
            entity.setBzbbnfzr(generaterSwapUtil.userSelectValues(entity.getBzbbnfzr()));
            entity.setXbr(generaterSwapUtil.userSelectValues(entity.getXbr()));
            entity.setZbhxbr(generaterSwapUtil.userSelectValues(entity.getZbhxbr()));
            entity.setXbrbm(generaterSwapUtil.comSelectValues(entity.getXbrbm()));
            entity.setZbhxbrbm(generaterSwapUtil.comSelectValues(entity.getZbhxbrbm()));
            entity.setXbrbmfzr(generaterSwapUtil.userSelectValues(entity.getXbrbmfzr()));
            entity.setZbhxbrbmfzr(generaterSwapUtil.userSelectValues(entity.getZbhxbrbmfzr()));
        }
        List<ZXGL_RenWuZhuanBanListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_RenWuZhuanBanListVO.class);
        for (ZXGL_RenWuZhuanBanListVO zXGL_RenWuZhuanBanVO : listVO) {
        }


        for (ZXGL_RenWuZhuanBanListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_RenWuZhuanBanPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_RenWuZhuanBanCrForm
     * @return
     */
    @ApiOperation("添加任务转办信息")
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_RenWuZhuanBanCrForm zXGL_RenWuZhuanBanCrForm) throws DataException {
        String visualId = "667627177688177861";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();

        // region 赋值部门负责人和领导
        ZXGL_XiangMuFenJie_mingxibiaoEntity model = zxglXiangMuFenJieMingxibiaoService.getInfo(zXGL_RenWuZhuanBanCrForm.getRwid());
        zXGL_RenWuZhuanBanCrForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
        zXGL_RenWuZhuanBanCrForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
        zXGL_RenWuZhuanBanCrForm.setZbbmfzr(model.getDepartmentManagerZzbm());
        // endregion

        ZXGL_RenWuZhuanBanEntity entity = JsonUtil.getJsonToBean(zXGL_RenWuZhuanBanCrForm, ZXGL_RenWuZhuanBanEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());

        zXGL_RenWuZhuanBanService.save(entity);
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_RenWuZhuanBanCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_RenWuZhuanBanCrForm, userInfo, zXGL_RenWuZhuanBanCrForm.getCandidateList());
                FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(mainId);
                if (flowTaskEntity != null) {
                    flowTaskEntity.setTitle(zXGL_RenWuZhuanBanCrForm.getZbrw() + "-任务转办-" + userInfo.getUserName() + "-" + DateUtil.today());
                    flowTaskEntity.setDataTable("zxgl_renwuzhuanban");
                    flowTaskService.update(flowTaskEntity);
                }
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("任务转办信息")
    @GetMapping("/{id}")
    public ActionResult<ZXGL_RenWuZhuanBanInfoVO> info(@PathVariable("id") String id) {
        ZXGL_RenWuZhuanBanEntity entity = zXGL_RenWuZhuanBanService.getInfo(id);
        ZXGL_RenWuZhuanBanInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_RenWuZhuanBanInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @ApiOperation("修改任务转办信息")
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_RenWuZhuanBanUpForm zXGL_RenWuZhuanBanUpForm) throws DataException {
        String visualId = "667627177688177861";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_RenWuZhuanBanEntity entity = zXGL_RenWuZhuanBanService.getInfo(id);
        if (entity != null) {

            // region 赋值部门负责人和领导
            ZXGL_XiangMuFenJie_mingxibiaoEntity model = zxglXiangMuFenJieMingxibiaoService.getInfo(zXGL_RenWuZhuanBanUpForm.getRwid());
            zXGL_RenWuZhuanBanUpForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
            zXGL_RenWuZhuanBanUpForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
            zXGL_RenWuZhuanBanUpForm.setZbbmfzr(model.getDepartmentManagerZzbm());
            // endregion

            ZXGL_RenWuZhuanBanEntity subentity = JsonUtil.getJsonToBean(zXGL_RenWuZhuanBanUpForm, ZXGL_RenWuZhuanBanEntity.class);

            zXGL_RenWuZhuanBanService.update(id, subentity);

            if (FlowStatusEnum.submit.getMessage().equals(zXGL_RenWuZhuanBanUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_RenWuZhuanBanUpForm.getCandidateList());
                    FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(id);
                    if (flowTaskEntity != null) {
                        flowTaskEntity.setTitle(zXGL_RenWuZhuanBanUpForm.getZbrw() + "-任务转办-" + userInfo.getUserName() + "-" + DateUtil.today());
                        flowTaskEntity.setDataTable("zxgl_renwuzhuanban");
                        flowTaskService.update(flowTaskEntity);
                    }
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除任务转办信息")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_RenWuZhuanBanEntity entity = zXGL_RenWuZhuanBanService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_RenWuZhuanBanService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


    /**
     * 任务转办流程更新
     *
     * @param flowWriteBack
     * @return
     */
    @ApiOperation("任务转办流程回调")
    @PostMapping("/updateFlow")
    @Transactional
    public ActionResult updateLX(@RequestBody ZXGL_FlowWriteBack flowWriteBack) throws DataException {
        boolean update = zXGL_RenWuZhuanBanService.updateFlow(flowWriteBack.getTaskId());
        if (update) {
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /* *//***
     * 转办流程结束后更信息任务库
     * @param zxglRenWuZhuanBanInfoVO
     * @return
     *//*
    @PostMapping("/updateFenJieMingXI")
    @Transactional
    public  ActionResult updateFenJieMingXI(@RequestBody ZXGL_RenWuZhuanBanInfoVO zxglRenWuZhuanBanInfoVO){
        return ActionResult.success(zXGL_RenWuZhuanBanService.updateFenJieMingXI(zxglRenWuZhuanBanInfoVO));
    }*/

}
